package com.bindapp.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.bindapp.R;
import com.bindapp.databinding.ActivityBaseBinding;
import com.bindapp.listener.ViewClickListener;
import com.bindapp.model.LoadLayoutModel;
import com.bindapp.util.AppManager;

/**
 * Created by apple on 2018/5/29.
 */

public class BaseActivity extends AppCompatActivity {
    protected ActivityBaseBinding baseBinding;
    protected LocationManager locationManager;
    public Location location;
    protected LoadLayoutModel loadLayoutModel;
    protected ProgressDialog progressDialog;
    protected double Xlocation = 0,Ylocation = 0;
    public int REQUEST_CODE=1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        baseBinding= DataBindingUtil.setContentView(this, R.layout.activity_base);
        setSupportActionBar(baseBinding.toolbar);
        initLoadModel();
        AppManager.addActivity(this);
        after();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.finishActivity(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        initMenu(menu);
        return super.onCreateOptionsMenu(menu);
    }

    private void initLoadModel(){
        loadLayoutModel=new LoadLayoutModel(this,new LoadClick());
    }

    protected void initMenu(Menu menu){
    }


    protected void after(){}

    protected void AddContentView(View view){
        baseBinding.content.removeAllViews();
        baseBinding.content.addView(view);
    }

    protected void handerBackToolar(String title){
        handerBackToolar(title, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    protected void handerBackToolar(String title,View.OnClickListener listener){
        //getmNavButtonView();
        //baseBinding.toolbar.setTitle(title);
        //baseBinding.toolbar.setTitleMargin(-20,0,0,0);
        baseBinding.toolbar.setSubtitle("");
        baseBinding.toolbar.setTitle("");
        baseBinding.titleview.setText(title);
        setSupportActionBar(baseBinding.toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        Drawable drawable = baseBinding.toolbar.getNavigationIcon();//
        drawable.setColorFilter(getResources().getColor(R.color.grey_color4), PorterDuff.Mode.SRC_ATOP);
        getSupportActionBar().setHomeAsUpIndicator(drawable);
        baseBinding.toolbar.setTitleTextColor(ContextCompat.getColor(this,R.color.grey_color4));
        baseBinding.toolbar.setNavigationOnClickListener(listener);

    }

    public void showProgress(String msg){
        if(progressDialog!=null){
            progressDialog=null;
        }
        progressDialog=new ProgressDialog(this);
        progressDialog.setCancelable(false);
        progressDialog.setMessage(msg);
        progressDialog.show();
    }
    public void dissProgress(){
        if(progressDialog!=null){
            progressDialog.dismiss();
        }
    }

    protected void showToast(String msg){
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }

    public void reLoad(View v) {}

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == Activity.RESULT_OK){
            onResult(requestCode,data);
        }
    }

    public void onResult(int requestCode,Intent data){}


    class LoadClick implements ViewClickListener {

        @Override
        public void reRequest(View view) {
            reReques();
        }
    }
    public void reReques(){}

}